package com.csun.cmny.provider.controller;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.vo.GroupVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcOrderService;
import com.csun.cmny.provider.service.OmcServiceService;
import com.csun.cmny.provider.service.UacRpcService;
import com.csun.cmny.provider.vo.PercentVo;
import com.csun.cmny.provider.vo.VolunteerVo;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.google.common.collect.Lists;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.*;

@RestController
@RequestMapping("/volunteer")
public class VolunteerController extends BaseController {

    @Resource
    private UacRpcService uacRpcService;
    @Resource
    private OmcOrderService omcOrderService;
    @Resource
    private OmcServiceService omcServiceService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;
    /*
    * 大数据平台社区志愿者服务
    * */
    @RequestMapping(value = "/selectByVolunteer",method = RequestMethod.POST)
    public Wrapper selectByVolunteer(){
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        //查询志愿者人数
        Integer count = uacRpcService.selectVolunteerCount(loginAuthDto.getGroupId());
        //查询服务时长
        Integer serviceTime = omcOrderService.selectServiceTime(loginAuthDto.getGroupId());
        //查询该平台下服务项目
        List<String> services = omcServiceService.selectSeriviceItem(loginAuthDto.getGroupId());
        //查询服务项目占比
        Map<String, Number> volunteerService = null;
        if(services.size() > 0){
            volunteerService = omcServiceService.selectServicePercent(loginAuthDto.getGroupId(),services);
        }
        VolunteerVo volunteerVo = new VolunteerVo();
        volunteerVo.setVolunteers(count);
        volunteerVo.setServiceTime(serviceTime);
        volunteerVo.setVolunteerService(volunteerService);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),volunteerVo);
    }

    /*
     * 大数据平台社区志愿者页面志愿者服务
     * */
    @PostMapping("/selectServiceByNurseHome")
    public Wrapper selectServiceByNurseHome(){
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        /**
         * @date    2021-11-19
         * @author  chisj
         * @desc    获取子机构id集合
         */
        List<GroupVo> groupVoList = uacRpcService.queryGroupListByGroupId(loginAuthDto.getGroupId());
        if (PublicUtil.isEmpty(groupVoList)) {
            return null;
        }

        List<Long> groupIdList = Lists.newArrayList();
        for (GroupVo gv : groupVoList) {
            groupIdList.add(gv.getId());
        }

        //查询该平台下服务项目
        List<String> services = omcServiceService.selectSeriviceItem(loginAuthDto.getGroupId());
        List<Map<String, Number>> volunteerService = null;
        List<VolunteerVo> volunteerVos = new ArrayList<>();
        if(services.size() > 0){
            volunteerService = omcServiceService.selectServiceByNurseHome(loginAuthDto.getGroupId(),services);
            for (Map<String, Number> m:volunteerService) {
                List<PercentVo> percentVos = new ArrayList<>();
                DecimalFormat df = new DecimalFormat("0.00%");
                PercentVo percentVo = new PercentVo();
                VolunteerVo vo = new VolunteerVo();
                vo.setGroupId(m.get("group_id").longValue());
                for (GroupVo g:groupVoList) {
                    if (vo.getGroupId().equals(g.getId())){
                        vo.setGroupName(g.getGroupName());
                    }
                }
                vo.setService(m.get("总服务次数").intValue());
                m.remove("group_id");
                m.remove("group_name");
                m.remove("总服务次数");
                for (String s:m.keySet()) {
                    percentVo = new PercentVo(s, m.get(s).intValue(),df.format(m.get(s).doubleValue()/vo.getService()));
                    percentVos.add(percentVo);
                }
                vo.setPercentVos(percentVos);
                volunteerVos.add(vo);
            }
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),volunteerVos);
    }

    /*
     * 大数据平台社区志愿者页面志愿者服务时长
     * */
    @PostMapping("/selectServiceTimeByNurseHome")
    public Wrapper selectServiceTimeByNurseHome(@RequestBody VolunteerVo vo){
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        vo.setGroupId(loginAuthDto.getGroupId());
        List<VolunteerVo> volunteerVos = omcServiceService.selectServiceTimeByNurseHome(vo);
        List<VolunteerVo> vos = new ArrayList<>();
        if(volunteerVos.size()>0){
            Set<String> groupNames = new HashSet<>();
            for (VolunteerVo volunteerVo:volunteerVos) {
                groupNames.add(volunteerVo.getGroupName());
            }
            for (String s: groupNames) {
                VolunteerVo vol = new VolunteerVo();
                vol.setGroupName(s);
                for (VolunteerVo volunteerVo:volunteerVos) {
                    if (volunteerVo.getGroupName().equals(s)){
                        vol.getVos().add(volunteerVo);
                    }
                }
                vos.add(vol);
            }

        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),vos);
    }

}
